#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *mystrtok(char *str, const char *delim)
{
	static char *p = NULL;
	int len = 0;
	char *pBak = NULL;
	int i;
	
	if (str != NULL)
		p = str;

	pBak = p;

	len = strlen(delim);

Start:
	while (*p) 
	{
		//和分割字符串元素依次比较
		for (i = 0; i < len; ++i)
		{
			if (delim[i] == *p) //找到符合分割点
			{
				*p = '\0';
				
				//如果pBak指向的字符即被分割的字符，后移一位
				if (pBak == p)
				{
					++p;
					pBak = p;
					goto Start;
				}
				++p;

				return pBak;
			}
		}

		//指针后移
		++p;
	}
	
	//遇到\0返回
	if(p != pBak)
	  return pBak;

	return (char*)NULL;
}

int main(void)
{
	char str[128] = "test string for new string just test string";
	char delim[10] = "str";
	char *p = NULL;

	printf("origin = %s\tdelim = %s\n",str,delim);

	p = mystrtok(str,delim);
	if (p)
	  printf("%s\n",p);

	while ((p = mystrtok(NULL,delim)))
	  printf("%s\n",p);

	return 0;
}
